package com.example.dell.basic;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dell.basicapplication.R;
import com.example.dell.utils.PhoneTypeUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;


/**
 * 普通的基本的basic类
 */

public abstract class BasicBasicActivity extends BasicAllActivity {

    private RelativeLayout left;
    private TextView       titletext;
    private RelativeLayout right;
    private LayoutInflater mInflater;
    private View           mNormalView;
    private FrameLayout    contentLayout;

    private LinearLayout              mBasicBar;
    private View                      mWaitView;
    private View                      mNoNetView;
    private View                      mNoDataView;
    private SmartRefreshLayout        mRefrsh;
    private SmartRefreshLayout        mNoRefrsh;
    private LinearLayout.LayoutParams mLayoutParams;
    private LinearLayout              mActionBarHight;

    public LinearLayout getBasicBar() {
        return mBasicBar;
    }

    public void setBasicBar(LinearLayout basicBar) {
        mBasicBar = basicBar;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置沉寝式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 全透明实现
            //getWindow.setStatusBarColor(Color.TRANSPARENT)
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4 全透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.activity_basic_basic);

        //如果actionBar为0的时候
        if (AppComplication.actionBarSize == 0) {
            AppComplication.actionBarSize = getStatusBarHeight();
        }
        mActionBarHight = (LinearLayout) findViewById(R.id.action_bar_set);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                AppComplication.actionBarSize);
        mActionBarHight.setLayoutParams(lp);


        left = (RelativeLayout) findViewById(R.id.left);
        titletext = (TextView) findViewById(R.id.titletext);
        right = (RelativeLayout) findViewById(R.id.right);
        contentLayout = (FrameLayout) findViewById(R.id.content_layout);
        mBasicBar = (LinearLayout) findViewById(R.id.basic_actionbar);


        //引入布局
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //修改手机栏上标题文字的颜色
        Activity parent = BasicBasicActivity.this;
        PhoneTypeUtil.setLightStatusBar(parent, true);


        mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);



        //加载正常界面前的等待状态
        mWaitView = mInflater.inflate(R.layout.wait_layout, null);

        //无网界面
        mNoNetView = mInflater.inflate(R.layout.nonet_layout, null);


        //无数据状态
        mNoDataView = mInflater.inflate(R.layout.page_error, null);


        //正常界面
        mNormalView = mInflater.inflate(setContentSuccessView(), null);
        initView(mNormalView);



        mRefrsh = (SmartRefreshLayout) mNoNetView.findViewById(R.id.refreshLayout);
        mRefrsh.setEnableLoadMore(false);
        mRefrsh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mNoRefrsh.finishRefresh(0);
                mCurrentState = STATE.LOADING;
                changeView();
                initData();

            }
        });


        mNoRefrsh = (SmartRefreshLayout) mNoDataView.findViewById(R.id.uichengerefrash);
        mNoRefrsh.setEnableLoadMore(false);
        mNoRefrsh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mNoRefrsh.finishRefresh(0);
                mCurrentState = STATE.LOADING;
                changeView();
                initData();

            }
        });
        mNoDataView.findViewById(R.id.btn_reload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });

        //默认设置
        setLeftShow(1, R.mipmap.back);


        //将四个布局添加在一起
        //加载成功的界面
        contentLayout.addView(mNormalView, mLayoutParams);
        //加载中的界面
        contentLayout.addView(mWaitView, mLayoutParams);
        //无数据界面
        contentLayout.addView(mNoDataView, mLayoutParams);
        //无网界面
        contentLayout.addView(mNoNetView, mLayoutParams);

        //页面切换的方法
        changeView();
        //根据网络数据，自动切换页面
        //autoShowPager();
        //初始化数据
        initData();


    }

    /**
     * 自动切换Ui，网络请求完，需要调用该方法来切换Ui,再initData中使用，网络请求完成数据后，
     *
     * @param haveNet 当前是否有网无网传 false ，有网的时候传true
     * @param obj     ，请求失败后传null ，如果有数据就传一个部位空的对象
     */
    protected void autoShowPager(boolean haveNet, final Object obj) {
        //如果是无网状态
        if (!haveNet) {
            mCurrentState = STATE.NONET;
            changeView();
            return;
        } else {
            //如果是有网状态,子线程获取数据
            //获取网络数据
            mCurrentState = checkData(obj);
            //主线程切换Ui
            changeView();
        }
    }


    private STATE checkData(Object obj) {
        if (!isConnected()) {
            return STATE.NONET;
        } else {
            if (obj == null) {//如果数据为空，则失败
                return STATE.ERROR;
            } else {
                if (obj instanceof List) {//如果返回的是数组
                    List list = (List) obj;
                    if (list.size() > 0) {
                        return STATE.SUCCESS;
                    } else {
                        return STATE.ERROR;
                    }
                } else {//如果是对象
                    return STATE.SUCCESS;
                }
            }
        }
    }


    //定义三个状态
    private enum STATE {
        LOADING,//正在加载中
        SUCCESS,//加载成功
        ERROR,//加载失败
        NONET,;//无网界面

    }

    //定义一个当前状态（默认正在加载中）
    private STATE mCurrentState = STATE.LOADING;

    private void changeView() {
        //先将三个页面都隐藏
        mWaitView.setVisibility(View.GONE);
        mNoDataView.setVisibility(View.GONE);
        mNoNetView.setVisibility(View.GONE);
        mNormalView.setVisibility(View.GONE);
        //根据当前状态切换
        switch (mCurrentState) {
            case LOADING:
                mWaitView.setVisibility(View.VISIBLE);
                break;
            case SUCCESS:
                mNormalView.setVisibility(View.VISIBLE);
                break;
            case ERROR:
                mNoDataView.setVisibility(View.VISIBLE);
                break;
            case NONET:
                mNoNetView.setVisibility(View.VISIBLE);
                break;
        }

    }


    /**
     * @return 添加正确的布局
     */
    protected abstract int setContentSuccessView();

    /**
     * 初始化view
     */
    protected abstract void initView(View view);

    /**
     * 进行赋值设置
     */
    protected abstract void initData();

    /**
     *
     * @param isNeed 是否许愿全沉寝式
     * @param color 设置颜色
     */
    protected void showAction(boolean isNeed,int color){
        if (isNeed){
            mActionBarHight.setVisibility(View.VISIBLE);
            mActionBarHight.setBackgroundColor(color);
        }else {
            mActionBarHight.setVisibility(View.GONE);
        }
    }
    /**
     * @param tag
     * @param resID 点击左面的返回按钮
     */
    protected void setLeftShow(int tag, int resID) {
        if (null == left) {
            left = (RelativeLayout) findViewById(R.id.left);
        }
        left.removeAllViews();
        left.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            left.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            left.addView(view, params);
        } else if (tag == 3) {
            View view = getLayoutInflater().inflate(R.layout.weightback, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            left.addView(view, params);
        }
    }

    /**
     * @param title 设置标题
     */
    protected void setTitleText(String title) {

        if (titletext == null) {
            titletext = (TextView) findViewById(R.id.titletext);
        }
        titletext.setText(title);
    }

    /**
     * 设置右边边按钮里是文字 setRightShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    protected void setRightShow(int tag, int resID) {
        if (null == right) {
            right = (RelativeLayout) findViewById(R.id.right);
        }
        right.removeAllViews();
        right.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            right.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            right.addView(view, params);
        }
    }

    public RelativeLayout getLeft() {
        return left;
    }

    public TextView getTitletext() {
        return titletext;
    }

    public RelativeLayout getRight() {
        return right;
    }

    /**
     * 利用反射获取状态栏高度
     *
     * @return
     */
    private int getStatusBarHeight() {
        int result = 0;
        //获取状态栏高度的资源id
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
